package com.kpiJavaTraining.bogdan.module2;

import java.io.File;
import java.io.IOException;

/**
 * Class checks for the existence user in the database of registered users and the availability of uncompleted test
 * @author Bogdan Olexandr
 */
public class Checker {
	
	private User user;
	
	/**
	 * method checks for the existence user in the database of registered users and returns a result
	 * @param user
	 * @return boolean 
	 * @throws IOException
	 */
	public boolean identification(String user) throws IOException{
		FileReader fileReader = new FileReader("users.usr");
		while(fileReader.hasNext()){
			String string = fileReader.read();
			String[] s = string.split(" ");
			if(s[0].equals(user)){		
				fileReader.close();
				return true;
			}
		}
		fileReader.close();
		return false;
	}
	
	/**
	 * method checks for the existence user in the database of registered users and returns a result
	 * @param user
	 * @return boolean 
	 * @throws IOException
	 */
	public boolean authenticationIs(String login, String password) throws IOException{
		FileReader fileReader = new FileReader("users.usr");
		while(fileReader.hasNext()){
			String string = fileReader.read();
			String[] s = string.split(" ");
			String loginPassword = s[0]+" "+s[1];
			if(loginPassword.equals(login+" "+password)){		
				user = new User(login,s[2]);
				fileReader.close();
				return true;
			}
		}
		fileReader.close();
		return false;
	}
	
	/**
	 * The method checks existence of uncompleted test and returns the result
	 * @param user
	 * @return boolean
	 */
	public boolean hasUncompleteTest(User user){
		File file = new File(user.toString()+".ucl");
		return file.exists();
	}
	
	/**
	 * method returns user
	 * @return User
	 */
	public User getUser(){
		return user;
	}	
}
